/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_RTMP_UTIL_H
#define MK_RTMP_UTIL_H
#include "MkUtil/MkTypes.h"

#define RtmpSigSize  1536
#define RtmpVersion  3
#define RtmpDefaultChunkSize  128
#define RtmpDefaultStreamId     0

typedef enum _rtmp_protocol
{
    ProtocolRtmpUnDefined,
    ProtocolRtmp,
    ProtocolRtmps,
    ProtocolRtmpt,
    ProtocolRtmpe,
    ProtocolRtmpte,
    ProtocolRtmpts,
    ProtocolRtmfp
}MkRtmpProtocol;

typedef enum _rtmp_chunk_id
{
    ChunkControl = 2,
    ChunkSystem = 3,
    ChunkSource = 4,
    ChunkResponse = 5,
    ChunkMedia = 8
}MkRtmpChunkId;

typedef enum _rtmp_commond_type
{
    RtmpMsgInvalid = 0,
    RtmpMsgChunkSize = 1,
    RtmpMsgAbort = 2,
    RtmpMsgAcknowledgement = 3,
    RtmpMsgControlEvent = 4,
    RtmpMsgWindownAcknowledgementSize = 5,
    RtmpMsgSetPeerBandWith = 6,
    RtmpMsgAudio = 8,
    RtmpMsgVideo = 9,
    RtmpMsgDataAmf3 = 15,
    RtmpMsgShareAmf3 = 16,
    RtmpMsgAmf3 = 17,
    RtmpMsgDataAmf0 = 18,
    RtmpMsgShareAmf0 = 19,
    RtmpMsgAmf0 = 20,
    RtmpMsgAggregate = 22
}MkRtmpCommandType;

typedef enum _rtmp_control_event
{
    ControlEventStreamBegin = 0,
    ControlEventStreamEof = 1,
    ControlEventStreamDry = 2,
    ControlEventSetBufferLen = 3,
    ControlEventStreamIsRecored = 4,
    ControlEventPingRequest = 6,
    ControlEventPingResponse = 7,
    ControlEventBufferEmpty = 31,
    ControlEventBufferReady = 32
}MkRtmpControlEvent;

typedef enum _rtmp_header_fmt_type
{
    MkRtmpHeaderFmt0,
    MkRtmpHeaderFmt1,
    MkRtmpHeaderFmt2,
    MkRtmpHeaderFmt3,
}MkRtmpHeaderFmtType;

typedef enum _rtmp_stream_id
{
    StreamControl,
}MkRtmpStreamId;

typedef enum _rtmp_limit_type
{

}MkRtmpLimitType;

typedef enum _rtmp_state
{
    RtmpStateInit = 0,
    RtmpStateC0 = 1,
    RtmpStateC1 = 2,
    RtmpStateS0 = 4,
    RtmpStateS1 = 8,
    RtmpStateS2 = 16,
    RtmpStateC2 = 32,
    RtmpStateClosed = 64
}MkRtmpState;

//find doc enum type
typedef enum _rtmp_pause_state
{
    RtmpPause0,
    RtmpPause1,
    RtmpPause2,
}MkRtmpPauseState;

typedef enum _rtmp_audio_support
{
    AudioSupportNone = 0x0001,
    AudioSupportAdpcm = 0x0002,
    AudioSupportMp3 = 0x0004,
    AudioSupportIntel = 0x0008, //not used
    AudioSupportUnused = 0x0010, //not used
    AudioSupportNelly8 = 0x0020,
    AudioSupportNelly = 0x0040,
    AudioSupportG711A = 0x0080,
    AudioSupportG711U = 0x0100,
    AudioSupportNelly16 = 0x0200,
    AudioSupportAac = 0x0400,
    AudioSupportSpeex = 0x0800,
    AudioSupportAll = 0x0FFF
}MkRtmpAudioSupport;

typedef enum _rtmp_video_support
{
    VideoSupportUnused = 0x01,
    VideoSupportJpeg = 0x02,
    VideoSupportSorenson = 0x04,
    VideoSupportHomeBrew = 0x08,
    VideoSupportVp6 = 0x10,
    VideoSupportVp6Alpha = 0x20,
    VideoSupportHomeBrewv = 0x40,
    VideoSupportH264 = 0x80,
    VideoSupportAll = 0xFF,
}MkRtmpVideoSupport;

typedef enum _rtmp_video_funciton
{
    SupportVideoClientSeek = 0x01
}MkRtmpVideoFunction;

typedef enum _rtmp_encoding_type
{
    EncodingAmf0 = 0x00,
    EncodingAmf3 = 0x03
}MkRtmpEncodingType;

typedef enum _rtmp_play_flag
{
    MkRtmpFlagAuth = 0x0001,    /* using auth param */
    MkRtmpFlagLive = 0x0002,    /* stream is live */
    MkRtmpFlagSwfv = 0x0004,    /* do SWF verification */
    MkRtmpFlagPlayList = 0x0008,    /* send playlist before play */
    MkRtmpFlagBuf = 0x0010,    /* toggle stream on BufferEmpty msg */
    MkRtmpFlagFreeTcUrl = 0x0020,    /* free tcUrl on close */
    MkRtmpFlagFreeApp = 0x0040     /* free app on close */
}MkRtmpPlayFlag;

typedef struct _rtmp_media_param
{
    Uint32 VideoWidth;
    Uint32 VideoHeight;
    Uint32 VideoFrameRate;
    Uint32 VideoBitRate;
    BOOL AudiobStereo;
    Uint32 AudioSamleRate;
    Uint32 AudioSampleSize;
    Uint32 AudioBitRate;
}MkMediaParam;

typedef struct _rtmp_header
{
    Uint32              TimeStamp;
    MkRtmpHeaderFmtType HeaderType;
    MkRtmpCommandType   CmdType;
    Uint32              ChunkId;
    Uint32              BodySize;
    Uint32              ByteReaded;
    Uint32              StreamId;
    _rtmp_header()
    {
        TimeStamp = 0;
        HeaderType = MkRtmpHeaderFmt0;
        CmdType = RtmpMsgInvalid;
        ChunkId = 0;
        BodySize = 0;
        ByteReaded = 0;
        StreamId = 0;
    }
}MkRtmpHeader;

class MkApi CMkRtmpUtil
{
public:
    CMkRtmpUtil();
    ~CMkRtmpUtil();
    static CMkRtmpUtil& GetInstance() { return m_Instance; }
    static Uint32 ParseRtmpUrl(const MkString& Url, MkRtmpProtocol& Protocol, MkString& Domain, Uint16& Port, MkString& AppName, MkString& Path);

    Uint32 GetProtocol(const MkString& strProtocol, MkRtmpProtocol& Protocol);
protected:
private:
    static CMkRtmpUtil  m_Instance;
    MkUnorderedMap<MkString, MkRtmpProtocol> m_MapRtmpProtocol;
};

#endif
